import { getWebCastPage } from '../../../api/webCast'
import { listByKind, findByPCateId } from '../../../api/base'

Page({
  data: {
    loading: false,
    list: [],
    listQuery: {
      condition: {
        keyword: '',
        categoryCodes: [],
        excludeIds:''
      },
      pager: {
        page: 1,
        pageSize: 10
      }
    },
    isEnd: false,
    isNoData: false,
    listHeight: 0,
    stateList: [],
    subjectList: [],
    
    subject: '',
    menuTitle: '全部学段',
    menuActiveIndex: 0,
    menus: [],
    menuActive: '',
    options: [],
  },
  options: {
    styleIsolation: 'shared'
  },
  onLoad: function (options) {
    this.setListHeight()
    // 科目
    listByKind('subject').then(res => {
      if (res.length) {
        res.forEach(r => {
          r.text = r.name
          r.value = r.code
        })
        res.unshift({
          text: '全部学科',
          value: ''
        })
      }
      this.setData({
        subjectList: res
      })
    })
    this.getMenus()
    this.init()
  },
  openDropdownItemState() {
    const query = wx.createSelectorQuery().in(this)
    query.select('.condition-wrap').boundingClientRect()
    query.exec(res => {
      let item = this.selectComponent('.custom-dropdown-item-state')
      item.setData({
        wrapperStyle: 'z-index: 100; top: ' + res[0].bottom + 'px'
      })
    })
  },
  openDropdownItemSubject(e) {
    const query = wx.createSelectorQuery().in(this)
    query.select('.condition-wrap').boundingClientRect()
    query.exec(res => {
      let item = this.selectComponent('.custom-dropdown-item-subject')
      item.setData({
        wrapperStyle: 'z-index: 100; top: ' + res[0].bottom + 'px'
      })
    })
  },
  setListHeight() {
    const _this = this
    const { windowHeight } = wx.getSystemInfoSync();
    const query = wx.createSelectorQuery().in(this)
    query.exec((res) => {
      _this.setData({
        listHeight: windowHeight - _this.selectComponent('#navBar').data.navHeight - 86
      })
    })
  },
  changeSubject(e) {
    let categoryCodes = []
    let stateList = this.data.options
    stateList.forEach(state => {
      if (state.isChecked) {
        categoryCodes.push(state.itemCode)
      }
    })
    if (e.detail!=='') {
      categoryCodes.push(e.detail)
    }

    this.setData({
      'listQuery.condition.categoryCodes': categoryCodes,
      'listQuery.pager.page': 1,
      list: [],
      isEnd: false,
      isNoData: false
    })
    this.init()
  },
  getMenus(id) {
    const _this = this
    // 获取菜单列表
    this.getCate(id).then(res => {
      if (res.categoryVoList.length) {
        res.categoryVoList.forEach(cate => {
          cate.text = cate.itemName;
          cate.id = cate.cateId;
        })
        _this.setData({
          menus: res.categoryVoList
        })
        let activeIndex = 0;
        // 获取二级菜单标签
        _this.getCate(res.categoryVoList[0].cateId).then(subRes => {
          if (subRes.categoryVoList && subRes.categoryVoList.length) {
            subRes.categoryVoList.forEach((cate, index) => {
              cate.text = cate.itemName;
              cate.id = cate.cateId;
              cate.isChecked = false
            })
            _this.setData({
              options: subRes.categoryVoList
            })
          }
        })
      }
    })
  },
  selectMenu(e) {
    const _this = this
    this.getCate(this.data.menus[e.detail.index].id).then(res => {
      if (res.categoryVoList && res.categoryVoList.length) {
        res.categoryVoList.forEach(cate => {
          cate.text = cate.itemName;
          cate.id = cate.cateId;
          cate.isChecked = false;
        })
        _this.setData({
          options: res.categoryVoList
        })
      }
    })
  },
  selectOption(e) {
    const _this = this
    const options = _this.data.options
    options.forEach(option => {
      option.isChecked = false
    })
    options[e.currentTarget.dataset.index].isChecked = true;
    _this.setData({
      options: options,
      menuTitle: options[e.currentTarget.dataset.index].itemName
    })
    _this.selectComponent('.custom-dropdown-item-state').toggle();
    _this.getCate(this.data.options[e.currentTarget.dataset.index].cateId).then(res => {
      if (res.categoryVoList && res.categoryVoList.length) {
        res.categoryVoList.forEach(cate => {
          cate.text = cate.itemName,
          cate.value = cate.itemCode
        })
        res.categoryVoList.unshift({
          text: '全部学科',
          value: ''
        })
        _this.setData({
          subjectList: res.categoryVoList,
          subject: '',
          options: options
        })
      }
    })
    this.setData({
      'listQuery.condition.categoryCodes': [e.currentTarget.dataset.item.itemCode],
      'listQuery.pager.page': 1,
      list: [],
      isEnd: false,
      isNoData: false
    })
    this.init()
  },
  getCate(id) {
    return new Promise(resolve => {
      findByPCateId({
        bizTypeEnum: 'WEB_CAST',
        pCateId: !id ? '' : id
      }).then(res => {
        resolve(res)
      })
    })
  },
  init() {
    this.getMore()
  },
  getMore() {
    const _this = this
    _this.setData({ loading: true })
    getWebCastPage(this.data.listQuery).then(res => {
      let list = JSON.parse(JSON.stringify(_this.data.list))
      if (res.records.length) {
        list = list.concat(res.records)
      }
      this.setData({ 
        list: list,
        loading: false
       })
      if (res.records.length < 10) {
        this.setData({ isEnd: true })
      }
      if (!res.records.length) {
        this.setData({ isNoData: true })
      }
    })
  },
  onSearch(e) {
    console.log(e)
    this.setData({
      'listQuery.condition.keyword': e.detail,
      isEnd: false,
      isNoData: false,
      list: []
    })
    this.getMore()
  },
  toLower() {
    if (!this.data.isEnd) {
      if (!this.data.loading) {
        const page = this.data.listQuery.pager.page + 1
        this.setData({
          'listQuery.pager.page': page
        })
        this.getMore()
      }
    }
  }
})
